package com.lj.universalframework.widget

import android.app.Activity
import android.content.Context
import android.text.TextUtils
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import com.google.android.material.imageview.ShapeableImageView
import com.lj.universalframework.R
import com.lj.universalframework.utils.clickWithTrigger

/**
 * <p>文件名：LJNavigationBar
 * <p>创建者：LJ
 * <p>创建日期：2020\10\16 0016 14:46
 * <p>描述：自定义顶部导航栏
 */
class LJNavigationBar : ConstraintLayout {

    //主布局
    private lateinit var view: View

    //返回按钮
    lateinit var ivTitleBarReturn: ShapeableImageView//PressedImageView

    //标题
    private lateinit var tvTitleBarTitle: TextView

    //右边文字菜单
    lateinit var tvTitleBarRightMenu: PressedTextView

    //右边图片菜单
    lateinit var ivTitleBarRightMenu: PressedImageView

    constructor(context: Context) : super(context) {
        initView(context, null)
    }

    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {
        initView(context, attrs)
    }

    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    ) {
        initView(context, attrs)
    }

    /**
     * 初始化组件
     *
     * @param context
     */
    private fun initView(context: Context, attrs: AttributeSet?) {
        val titleBar = LayoutInflater.from(context).inflate(R.layout.title_bar, this)
        view = titleBar.findViewById(R.id.titlr_bar)
        ivTitleBarReturn = titleBar.findViewById(R.id.iv_titleBar_return)
        tvTitleBarTitle = titleBar.findViewById(R.id.tv_titleBar_title)
        tvTitleBarRightMenu = titleBar.findViewById(R.id.tv_titleBar_right_menu)
        ivTitleBarRightMenu = titleBar.findViewById(R.id.iv_titleBar_right_menu)

        attrs!!.apply {
            val typedArrat = context.obtainStyledAttributes(this, R.styleable.LJNavigationBar)
            typedArrat.apply {
                //左边返回按钮图标
                val leftSrc = getResourceId(R.styleable.LJNavigationBar_leftSrc, 0)
                //右边菜单按钮图标
                val rightSrc = getResourceId(R.styleable.LJNavigationBar_rightSrc, 0)
                //右边菜单按钮内容
                val rightText = getString(R.styleable.LJNavigationBar_rightText)
                //标题
                val title = getString(R.styleable.LJNavigationBar_title)
                //背景颜色
                val backgroundColor = getColor(R.styleable.LJNavigationBar_backgroundColor, 0)
                //标题颜色
                val titleColor =getColor(R.styleable.LJNavigationBar_titleColor, 0)
                //是否禁用默认返回按钮事件左边返回按钮图标
                val leftOnClick = getBoolean(R.styleable.LJNavigationBar_leftOnClick, false)
                //是否隐藏默认返回按钮
                val lefthide = getBoolean(R.styleable.LJNavigationBar_lefthide, false)
                //注意回收
                typedArrat.recycle()
                if (leftSrc != 0) {
                    ivTitleBarReturn.setImageResource(leftSrc)
                }
                if (rightSrc != 0) {
                    ivTitleBarRightMenu.setImageResource(rightSrc)
                    ivTitleBarRightMenu.visibility = View.VISIBLE
                }
                if (!TextUtils.isEmpty(rightText)) {
                    tvTitleBarRightMenu.text = rightText
                    tvTitleBarRightMenu.visibility = View.VISIBLE
                }
                if (!TextUtils.isEmpty(title)) {
                    tvTitleBarTitle.text = title
                }
                if (backgroundColor != 0) {
                    view.setBackgroundColor(backgroundColor)
                }
                if (titleColor != 0) {
                    tvTitleBarTitle.setTextColor(titleColor)
                }
                if (lefthide) {
                    ivTitleBarReturn.visibility = View.GONE
                }
                if (!leftOnClick) {
                    ivTitleBarReturn.clickWithTrigger {
                        try {
                            val activity: Activity = context as Activity
                            activity.finish()
                        } catch (e: Exception) {
                            e.message
                        }
                    }
                }
            }
        }
    }

    /**
     * 设置背景颜色
     */
    fun backgroundColor(color: Int) {
        view.setBackgroundColor(color)
    }

    /**
     * 设置标题
     */
    fun title(title: String) {
        tvTitleBarTitle.text = title
    }


    /**
     * 简单的一键设置
     */
    fun init(activity: Activity, title: String) {
        tvTitleBarTitle.text = title
        ivTitleBarReturn.clickWithTrigger {
            activity.finish()
        }
    }
}